﻿<%@ WebHandler Language="C#" Class="Handler" %>


using System;
using System.Web;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Text;
using System.Web;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "application/json";
        //context.Response.ContentType = "text/plain";
        int useridx = MediaUtil.ReturnMemberId();
        string userid = MediaUtil.ReturnUserID();
        //int useridx = 82656206;//MediaUtil.ReturnMemberId();
        //string userid = "yhw1984";
        
        string action = string.Empty;
        string str_s = string.Empty;
        string output = string.Empty;
        if (context.Request["action"] != null && context.Request["action"].Length > 0)
        {
            action = context.Request["action"].ToString();
        }
        else
        {
            return;
        }
        if (context.Request["b"] != null && context.Request["b"].Length > 0)
        {
            str_s = context.Request["b"].ToString();
        }
        else
        {
            return;
        }
       // useridx = 82656206;
        //userid = "yhw1984";
        if (useridx <= 0)
        {
            return;
        }

        string ndate = "2013-04-29";
        if (action == "lotery")            //领取
        {
            
            if (DateTime.Now >= DateTime.Parse("2013-4-29"))
            {
                int ret = lottery(1, useridx, userid, ndate, str_s);
                output = ret.ToString();
            }
            else
            {
                output = "-10";
            }
        }
        else if (action == "zjsome")      //是否领取
        {
            DataTable dt = lottery_dt(3, useridx, userid, ndate, str_s);

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    output += dt.Rows[i]["ntype"].ToString() + ",";
                }
                 
            }
        }
        else if (action == "load")      //排行帮
        {
            DataTable dt = lottery_dt(2, useridx, userid, ndate, str_s);
            string nn = "";
            if (dt.Rows.Count > 0)
            {
                nn = "<table>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //if (nn != "")
                    //{
                    //    nn = nn + "|";
                    //}

                    nn += "<tr><td style='width:160px'>" + dt.Rows[i]["myname"].ToString() + "</td><td>" + dt.Rows[i]["nums"].ToString() + "</td></tr>";
                     
                }
               nn += "</table>";
            }
            output = nn;
        }
        
        context.Response.Write(output);
    }

    #region 领取
    /// <summary>
    /// 抽奖
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static int lottery(int type, int useridx, string userid,string pp,string ll)
    {
        int ret = -2;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,50,userid),
                         SqlHelper.MakeInParam("@param1",SqlDbType.VarChar,20,pp),
                         SqlHelper.MakeInParam("@param2",SqlDbType.VarChar,20,ll),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)                  
                    };
        try
        {
            SqlHelper.ExecuteNonQuery(active.Conn, CommandType.StoredProcedure, "[As_Select_ProLabor]", p);
            ret = int.Parse(p[5].Value.ToString());
        }
        catch
        {

        }
        return ret;
    }
    /// <summary>
    /// 排行榜
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static DataTable lottery_dt(int type, int useridx, string userid, string pp, string ll)
    {
        int ret = -2;
        DataTable dt = new DataTable();
        if (HttpRuntime.Cache["active_gofishing"] == null)
        {
            SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,50,userid),
                         SqlHelper.MakeInParam("@param1",SqlDbType.VarChar,20,pp),
                         SqlHelper.MakeInParam("@param2",SqlDbType.VarChar,20,ll),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)                  
                    };
            try
            {
                dt = SqlHelper.ExecuteDataTable(active.Conn, CommandType.StoredProcedure, "[As_Select_ProLabor]", p);
                ret = int.Parse(p[5].Value.ToString());
            }
            catch
            {

            }
        }
        else
        {
            dt = (DataTable)HttpRuntime.Cache["active_gofishing"]; 
        }
        return dt;
    }
    
    
    #endregion
    
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}